package com.xh.bussiness.log.service.impl;

import com.xh.bussiness.log.bean.request.BizQuestionLogRequest;
import com.xh.bussiness.log.model.BizQuestionLog;
import com.xh.bussiness.log.dao.BizQuestionLogMapper;
import com.xh.bussiness.log.service.IBizQuestionLogService;
import com.xh.bussiness.tiku.bean.dto.QuestionDto;
import com.xh.bussiness.tiku.service.IQuestionService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * <p>
 * 学科网导入记录表 服务实现类
 * </p>
 *
 * @author TW
 * @since 2021-07-30
 */
@Service
public class BizQuestionLogServiceImpl extends BaseServiceImpl<BizQuestionLogMapper, BizQuestionLog> implements IBizQuestionLogService {

    @Autowired
    IQuestionService questionService;

    @Override
    public XhPage queryBizQuestionLogPage(XhPage page, BizQuestionLogRequest bean) {
        QueryWrapper qw=new QueryWrapper<BizQuestionLog>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryBizQuestionLog(BizQuestionLogRequest bean) {
        QueryWrapper qw=new QueryWrapper<BizQuestionLog>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    /**
     * 查询试题详情
     * @author 赵文博
     * @date 2021/8/3 14:43
     * @param bean
     * @return java.util.List
     **/
    @Override
    public List queryQuestionByRelationId(BizQuestionLogRequest bean) {
        QueryWrapper qw=new QueryWrapper<BizQuestionLog>();
        //拼装条件
        qw=prepareParams(bean, qw);
        qw.orderByDesc("create_time");
        BizQuestionLog log = this.selectFirst(qw);
        List<String> questionIds = Arrays.asList(log.getQuestionIds().split(","));
        List<QuestionDto> arrayList = new ArrayList<>();
        for(String questionId : questionIds){
            QuestionDto questionDto = questionService.getQuestion(questionId,null);
            if(questionDto!=null){
                arrayList.add(questionDto);
            }
        }
        return arrayList;
    }

    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(BizQuestionLogRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if(StringUtils.isNotEmpty(bean.getRelationId())){
            qw.in("relation_id",bean.getRelationId());
        }
        return qw;
    }
}
